package cn.innosmart.aq.bean;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by David on 2015/8/21.
 */
public class ScheduleBean {
    private String title;
    private String id;
    private int enabled;
    private String icon_data;
    private String icon_type;
    private JSONArray dates;
    private JSONArray timeline;
    private String place;
    private ArrayList<CommandBean> scheduleCommands;
    private String time;
    private String pns;
    public int pns_priority; // 0-不推送， 1-普通，2-重要，3-非常重要
    public ScheduleBean() {
    }

    public ScheduleBean(JSONObject jsonObject) {
        try {
            this.title = jsonObject.getString("title");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.id = jsonObject.getString("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.enabled = jsonObject.getInt("enabled");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.pns_priority = jsonObject.getInt("pns_priority");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.icon_data = jsonObject.getString("icon_data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.icon_type = jsonObject.getString("icon_type");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.place = jsonObject.getString("place");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.time = jsonObject.getString("time");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.pns = (String) jsonObject.getJSONArray("enabled_notifications").get(0);
        } catch (JSONException e) {
//            e.printStackTrace();
        }
        /**
         * 设置ArrayList<CommandBean> commands;
         */
        ArrayList<CommandBean> commands;
        CommandBean commandBean;
        JSONArray jsonRootCommands;
        JSONObject jsonRootCommand;
        JSONArray jsonCommands;
        JSONObject jsonCommand;
        ArrayList<ActionBean> command;
        ActionBean actionBean;
        try {
            jsonRootCommands = jsonObject.getJSONArray("commands");
            commands = new ArrayList<CommandBean>();
            for (int i = 0; i < jsonRootCommands.length(); i++) {
                jsonRootCommand = (JSONObject) jsonRootCommands.get(i);
                commandBean = new CommandBean();
                String type = jsonRootCommand.getString("type");
                if (type.equals("sleep")) {
                    commandBean.setOrder(jsonRootCommand.getInt("order"));
                    commandBean.setType(jsonRootCommand.getString("type"));
                    commandBean.setValue(jsonRootCommand.getInt("value"));
                    commands.add(commandBean);
                } else if (type.equals("commands")) {
                    commandBean.setType(jsonRootCommand.getString("type"));
                    commandBean.setOrder(jsonRootCommand.getInt("order"));
                    jsonCommands = jsonRootCommand.getJSONArray("commands");
                    command = new ArrayList<ActionBean>();
                    for (int j = 0; j < jsonCommands.length(); j++) {
                        actionBean = new ActionBean();
                        jsonCommand = (JSONObject) jsonCommands.get(j);
                        actionBean.setOrder(jsonCommand.getInt("order"));
                        actionBean.setField(jsonCommand.getString("field"));
                        actionBean.setValue(jsonCommand.getDouble("value"));
                        actionBean.setAddress(jsonCommand.getString("address"));
                        command.add(actionBean);
                    }
                    commandBean.setCommand(command);
                    commands.add(commandBean);
                } else if (type.equals("scene")) {
                    commandBean.setType("scene");
                    commandBean.setOrder(jsonRootCommand.getInt("order"));
                    commandBean.setScene_id(jsonRootCommand.getString("id"));
                    commands.add(commandBean);
                }
            }
            this.scheduleCommands = commands;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONArray jsonConvertTimeLine = new JSONArray();
            JSONArray jsonTimeLine = jsonObject.getJSONArray("timeline");
            jsonConvertTimeLine = jsonTimeLine;
            this.timeline = jsonConvertTimeLine;
            System.out.println("jsonConverTimeLine=" + jsonConvertTimeLine.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public String getPns() {
        return pns;
    }

    public void setPns(String pns) {
        this.pns = pns;
    }

    public String getIcon_type() {
        return icon_type;
    }

    public void setIcon_type(String icon_type) {
        this.icon_type = icon_type;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public JSONArray getTimeline() {
        return timeline;
    }

    public void setTimeline(JSONArray timeline) {
        this.timeline = timeline;
    }

    public String format(int value) {
        String tmpStr = String.valueOf(value);
        if (value < 10) {
            tmpStr = "0" + tmpStr;
        }
        return tmpStr;
    }

    public JSONArray getDates() {
        return dates;
    }

    public void setDates(JSONArray dates) {
        this.dates = dates;
    }

    public ArrayList<CommandBean> getScheduleCommands() {
        return scheduleCommands;
    }

    public void setScheduleCommands(ArrayList<CommandBean> scheduleCommands) {
        this.scheduleCommands = scheduleCommands;
    }

    public String getIcon_data() {
        return icon_data;
    }

    public void setIcon_data(String icon_data) {
        this.icon_data = icon_data;
    }

    public int getEnabled() {
        return enabled;
    }

    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public String toParams() {
        JSONObject jsRoot = new JSONObject();
        JSONArray jsonCommands = new JSONArray();
        ScheduleCommand scheduleCommand;
        try {
            jsRoot.put("title", title);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            jsRoot.put("enabled", enabled);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            jsRoot.put("pns_priority", pns_priority);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            jsRoot.put("place", place);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (id != null) {
            try {
                jsRoot.put("id", id);
            } catch (JSONException e) {

            }
        }
        try {
            jsRoot.put("icon_data", "icon_data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            jsRoot.put("icon_type", "icon_type");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            jsRoot.put("time", time);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            if (!TextUtils.isEmpty(pns))
                jsRoot.put("enabled_notifications", new JSONArray().put("pns"));
            else
                jsRoot.put("enabled_notifications", new JSONArray());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONArray command;
        JSONObject jsonAction;
        JSONObject jsonCommand;
        try {
            for (CommandBean commandBean : scheduleCommands) {
                jsonCommand = new JSONObject();
                command = new JSONArray();
                if (commandBean.getType().equals("commands")) {
                    jsonCommand.put("type", commandBean.getType());
                    jsonCommand.put("order", commandBean.getOrder());
                    ArrayList<ActionBean> actionBeans = commandBean.getCommand();
                    for (int j = 0; j < actionBeans.size(); j++) {
                        ActionBean actionBean = actionBeans.get(j);
                        jsonAction = new JSONObject();
                        jsonAction.put("order", actionBean.getOrder());
                        jsonAction.put("field", actionBean.getField());
                        jsonAction.put("value", actionBean.getValue());
                        jsonAction.put("address", actionBean.getAddress());
                        command.put(jsonAction);
                    }
                    jsonCommand.put("commands", command);
                } else if (commandBean.getType().equals("scene")) {
                    jsonCommand.put("type", commandBean.getType());
                    jsonCommand.put("order", commandBean.getOrder());
                    jsonCommand.put("id", commandBean.getScene_id());
                }
                jsonCommands.put(jsonCommand);
            }
            jsRoot.put("commands", jsonCommands);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONArray jsonConvertTimeLine = new JSONArray();
            jsonConvertTimeLine = timeline;
            System.out.println("jsonConverTimeLine=" + jsonConvertTimeLine.toString());
            jsRoot.put("timeline", jsonConvertTimeLine);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsRoot.toString();
    }

    public String toDeleteParams() {
        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonParams.toString();
    }
}
